package com.hpf.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hpf.lease.common.config.CommonException;
import com.hpf.lease.common.result.ResultCodeEnum;
import com.hpf.lease.model.bean.SystemUser;
import com.hpf.lease.web.admin.dto.SystemUserSearchSubmitDto;
import com.hpf.lease.web.admin.mapper.SystemUserMapper;
import com.hpf.lease.web.admin.service.SystemUserService;
import com.hpf.lease.web.admin.vo.SystemUserInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author GOD
* @description 针对表【system_user(员工信息表)】的数据库操作Service实现
* @createDate 2024-08-29 15:00:50
*/
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser>
    implements SystemUserService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateInfo(SystemUser user) {
        long id = user.getId() == null ? -1L : user.getId();
        SystemUser one = this.getOne(Wrappers.lambdaQuery(SystemUser.class)
                .eq(SystemUser::getUsername, user.getUsername()));
        if (one != null && !one.getId().equals(id)){
            throw new CommonException(ResultCodeEnum.ADMIN_ACCOUNT_EXIST_ERROR);
        }
        this.saveOrUpdate(user);
        return true;
    }

    @Override
    public Page<SystemUserInfoVo> getPageItem(SystemUserSearchSubmitDto submitDto) {
        Page<SystemUserInfoVo> page = new Page<>(submitDto.getCurrent(), submitDto.getSize());
        List<SystemUserInfoVo> list = baseMapper.getPageItem(page , submitDto);
        page.setRecords(list);
        return page;
    }

    @Override
    public SystemUserInfoVo getDetailById(long id) {
        return baseMapper.getDetailById(id);
    }
}




